package com.czl.module_storehouse.activity.intostore.project;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_base.activity.BaseActivity;
import com.czl.module_base.utils.ParcelHelper;
import com.czl.module_storehouse.databinding.ActivityViewOrderItemBinding;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.activity.intostore.AddWareHouseActivity;
import com.czl.module_storehouse.adapter.ViewStoreProjectItemAdapter;
import com.czl.module_storehouse.bean.StoreProjectBean;
import com.czl.module_storehouse.event.FacilityListEvent;
import com.czl.module_storehouse.event.StoreProjectEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xubin
 */
public class ViewStoreProjectItemActivity extends BaseActivity<ActivityViewOrderItemBinding> {

    private ViewStoreProjectItemAdapter mAdapter;
    private StoreProjectBean mStoreProjectBean;

    @Override
    protected ActivityViewOrderItemBinding getBinding(LayoutInflater layoutInflater) {
        return ActivityViewOrderItemBinding.inflate(layoutInflater);
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        mToolBinding.toolbarContentTitle.setText("入库物品");
        binding.llBottom.setVisibility(View.VISIBLE);
        mAdapter = new ViewStoreProjectItemAdapter(R.layout.item_view_order_item_pro, new ArrayList<>());
        binding.recyclerView.setAdapter(mAdapter);

        mAdapter.setOnItemChildClickListener((adapter, view, position) -> setTextSelectedEnableAndText());

        mAdapter.setOnItemClickListener((adapter, view, position) -> mAdapter.clickItem(mAdapter.getItem(position), position));

        setTextSelectedEnableAndText();

        EventBus.getDefault().register(this);
    }

    private void setTextSelectedEnableAndText() {
        int totalSelectedNum = mAdapter.getTotalSelectedNum();
        binding.tvSortCount.setText(getString(R.string.already_select_goods_num, totalSelectedNum));
        binding.textSelected.setEnabled(totalSelectedNum > 0);

        binding.checkBoxAll.setChecked((totalSelectedNum > 0 && totalSelectedNum == mAdapter.getData().size()));
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(FacilityListEvent event) {
        if (event == null) {
            return;
        }
        if (mAdapter != null) {
            mAdapter.addData(event.getFacilityListBeans());
        }
    }

    @Subscribe(threadMode = ThreadMode.POSTING, sticky = true)
    public void onDataEvent(StoreProjectBean event) {
        if (event == null) {
            return;
        }
        mStoreProjectBean = ParcelHelper.copy(event);

        List<SortBean> listByProject = getListByProject(mStoreProjectBean);
        if (mAdapter != null) {
            mAdapter.addData(listByProject);
        }
    }

    private List<SortBean> getListByProject(StoreProjectBean bean) {
        if (bean == null) {
            return new ArrayList<>();
        }
        List<SortBean> facilityList = bean.getFacilityList();

        List<SortBean> list = new ArrayList<>();
        if (facilityList != null && !facilityList.isEmpty()) {
            for (SortBean listBean : facilityList) {
                /*listBean.setAddSortType(ADD_TYPE_FACILITY);
                listBean.setSortModel(listBean.getModel());
                listBean.setSortName(listBean.getFacilityName());
                listBean.setPrice(listBean.getPrice());
                listBean.setStorageNum(listBean.getFacilitySum());
                listBean.setUnInStoreNum(listBean.getFacilitySum());
                listBean.setPropCompanyName(listBean.getParentCompanyName2());
                listBean.setPropCompanyShortName(listBean.getParentCompanyShortName2());
                listBean.setPropCompanyId(listBean.getParentCompanyId2());
                listBean.setManufacturer(listBean.getProduceCompanyName());
                listBean.setManufactureDate(listBean.getProduceDate());
                listBean.setMiniProject(true);
                listBean.setEneSortId(listBean.getFacilityPrepareId());*/
                //固定资产
                listBean.setSortType(2);
                list.add(listBean);
            }
        }
        List<SortBean> sortList = bean.getSortList();
        if (sortList != null && !sortList.isEmpty()) {
            for (SortBean sortBean : sortList) {
                /*sortBean.setAddSortType(ADD_TYPE_SORT);
                sortBean.setStorageNum(sortBean.getUnInStoreNumInt());
                sortBean.setPropCompanyId(sortBean.getParentCompanyId());
                sortBean.setPropCompanyName(sortBean.getParentCompanyName());
                sortBean.setPropCompanyShortName(sortBean.getParentCompanyShortName());
                sortBean.setManufacturer(sortBean.getProduceCompanyName());
                sortBean.setManufactureDate(sortBean.getProduceDate());
                sortBean.setEneSortId(sortBean.getSortPrepareId());
                //低耗
                sortBean.setSortType(1);
                sortBean.setMiniProject(true);*/
            }
            list.addAll(sortList);
        }
        return list;
    }

    public void onViewClicked(View view) {
        int id = view.getId();
        if (id == R.id.text_selected) {
            selectedSort();
        } else if (id == R.id.text_back) {
            finish();
        } else if (id == R.id.check_box_all) {
            mAdapter.setSelectedAll(binding.checkBoxAll.isChecked());
            setTextSelectedEnableAndText();
        }
    }

    /**
     * 添加选中后的物品
     */
    private void selectedSort() {
        if (mStoreProjectBean == null) {
            showToast("数据错误");
            return;
        }
        List<SortBean> list = new ArrayList<>();
        for (SortBean sortBean : mAdapter.getData()) {
            if (sortBean.selected()) {
                list.add(sortBean);
            }
        }
        mStoreProjectBean.setMateSortList(true);
        mStoreProjectBean.setMaterSortList(list);
        EventBus.getDefault().postSticky(new StoreProjectEvent(mStoreProjectBean));
        Intent intent = new Intent(getContext(), AddWareHouseActivity.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        startActivity(intent);
    }

}
